package com.tgy.ai;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.net.*;
import java.io.*;


public class ChatGPTClient {
    public static void main(String[] args) {
        String apiUrl = "https://api.openai.com/v1/chat/completions";
        String apiKey = "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";
        String prompt = "Please help me write a Java program to calculate the area of a circle. The program should prompt the user to enter the radius of the circle.";
        String model = "davinci-codex";
        String token = "YOUR_API_KEY";

        try {
            URI uri = new URI(apiUrl);
            HttpPost postRequest = new HttpPost(uri);

            postRequest.addHeader("Authorization", "Bearer " + apiKey);
            postRequest.addHeader("Content-Type", "application/json");

            String jsonBody = "{\n" +
                    "  \"model\": \"gpt-3.5-turbo\",\n" +
                    "  \"messages\": [{\"role\": \"user\", \"content\": \"java的基本数据类型有哪些?\"}],\n" +
                    "  \"temperature\": 0.7\n" +
                    "}";
            StringEntity entity = new StringEntity(jsonBody);
            postRequest.setEntity(entity);

            HttpHost proxy = new HttpHost("144.202.107.108", 1941);
            RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
            CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
            CloseableHttpResponse response = httpClient.execute(postRequest);
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println(responseBody);
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}
